package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pages.ProjectPage;
import com.ktjy.pojo.BasDict;
import com.ktjy.service.BasDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 客户等级管理
 */
@Controller
public class BasDictController {
    @Autowired
    BasDictService basDictService;

    @RequestMapping("/dict/list")
    public String dictList(Long dictId, String dictItem, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "3") Integer pageSize, Model model) {
        IPage<BasDict> dictIPage = new Page<>(pageIndex, pageSize);

        QueryWrapper<BasDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(dictId != null, "dict_id", dictId);
        queryWrapper.like(dictItem != null, "dict_item", dictItem);
        queryWrapper.eq("dict_type", "客户等级");
        Page<BasDict> page = (Page<BasDict>) basDictService.page(dictIPage, queryWrapper);
        ProjectPage<BasDict> projectPage = new ProjectPage<BasDict>();
        BeanUtils.copyProperties(page, projectPage);
        model.addAttribute("dictPage", projectPage);
        model.addAttribute("dictId", dictId);
        model.addAttribute("dictItem", dictItem);
        return "/dict/list";
    }

    @RequestMapping("/dict/add")
    public Object dictAdd() {
        return "/dict/add";
    }
    @RequestMapping("/dict/edit")
    public Object dictEdit(Long dictId,Model model) {
        model.addAttribute("dict",basDictService.getById(dictId));
        return "/dict/edit";
    }

    @RequestMapping("/dict/save")
    public Object dictSave(BasDict basDict) {
        if (!(basDict ==null)) {
            boolean flg = basDictService.saveOrUpdate(basDict);
            if (flg) {
                return "redirect:/dict/list";
            } else {
                return "/dict/add";
            }
        } else{
            return "/dict/add";
        }
    }
}
